package cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.simplenotice;

import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.NoticeStateChange;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import io.realm.Realm;

public class SampleNoticeDetailActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.img_msg_icon)
    ImageView mImgMsgIcon;
    @BindView(R.id.tv_msg_title)
    TextView mTvMsgTitle;
    @BindView(R.id.tv_msg_time)
    TextView mTvMsgTime;
    @BindView(R.id.tv_msg_content)
    TextView mTvMsgContent;

    long noticeId;

    @Override
    public int initContentView() {
        return R.layout.activity_sample_notice_detail;
    }

    @Override
    public void initInjector() {
        noticeId = getIntent().getLongExtra("notice", 0);
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());


        if (noticeId != 0) {
            fetchSampleNotice(noticeId);
        } else {
            ToastUtil.showToast("消息无效");
        }


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void fetchSampleNotice(final long noticeId) {
        Realm.getDefaultInstance().executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                fillData(realm.where(SampleNotice.class).equalTo(SampleNotice.NOTICE_FIELD_TIME, noticeId).findFirst());
            }
        });
    }


    private void fillData(SampleNotice notice) {
        mTvMsgTitle.setText(notice.getTitle());
        mTvMsgTime.setText(new SimpleDateFormat("HH:mm").format(notice.getTime()));
        mTvMsgContent.setText(notice.getContent());
        upMsgReadState(notice);
    }


    private void upMsgReadState(final SampleNotice notice) {
        notice.setReaded(true);
        EventBus.getDefault().post(new NoticeStateChange());
    }
}
